﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Moe_v0._1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void GenerateConversationFile()
        {
            Regex questionPattern = new Regex("^[a-zA-Z]+:.+\\?\\s*$");
            Regex answerPattern = new Regex("^[a-zA-Z]+:.+\\.\\s*$");
            int count = 0;
            using (StreamReader reader = new StreamReader(@".\Model\初中英语全部对话课文.txt"))
            {
                using (StreamWriter writer = new StreamWriter(@".\Model\Conversations.txt"))
                {
                    while (!reader.EndOfStream)
                    {
                        string question = reader.ReadLine();
                        string answer = "";
                        if (questionPattern.Match(question).Success)
                        {
                            if (!reader.EndOfStream)
                            {
                                do
                                {
                                    answer = reader.ReadLine();
                                } while (answer.Length == 0);
                                if (answerPattern.Match(answer).Success)
                                {
                                    writer.WriteLine(question);
                                    writer.WriteLine(answer);
                                    ++count;
                                }
                            }
                        }
                    }
                }
            }

            MessageBox.Show(string.Format("{0} question-answer pairs are found.", count));

        }
        static void GeneratePrefixModel()
        {
            Regex contentPattern = new Regex("\\w+:(?<content>.+)");
            using (StreamReader reader = new StreamReader(@".\Model\Conversations.txt"))
            {
                Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
                while (!reader.EndOfStream)
                {
                    string question = reader.ReadLine();
                    string answer = reader.ReadLine();
                    question = contentPattern.Match(question).Groups["content"].Value;
                    question = question.Remove(question.Length - 2);
                    answer = contentPattern.Match(answer).Groups["content"].Value;
                    if (dict.ContainsKey(question))
                    {
                        dict[question].Add(answer);
                    }
                    else
                    {
                        dict.Add(question, new List<string> { answer });
                    }
                }

                MessageBox.Show(string.Format("{0} question-answer pairs.", dict.Count));
                using (StreamWriter writer = new StreamWriter(@".\Model\QA2.txt"))
                {
                    string line = "";
                    foreach (var item in dict)
                    {
                        line = item.Key + "|";
                        foreach (var ans in item.Value)
                        {
                            line += ans + "|";
                        }
                        line = line.Remove(line.Length - 1);
                        writer.WriteLine(line);
                    }
                }
            }
        }
        static void TrimStartSpace()
        {
            List<string> lines = new List<string>();
            using (StreamReader reader = new StreamReader(@".\Model\QA.txt"))
            {
                while (!reader.EndOfStream)
                {
                    lines.Add(reader.ReadLine().TrimStart(' '));
                }
            }

            using (StreamWriter writer = new StreamWriter(@".\Model\QA.txt"))
            {
                foreach (var line in lines)
                {
                    var parts = line.Split('|').ToList();
                    string question = parts[ 0 ];
                    parts.Remove(question);
                    var mainQuestions = question.Split(",.?".ToCharArray());
                    question = "";
                    foreach (var q in mainQuestions)
                    {
                        if (q.Length > question.Length)
                        {
                            question = q;
                        }
                    }
                    question = question.TrimStart(' ') + "|";
                    foreach (var q in parts)
                    {
                        question += q.TrimStart(' ') + "|";
                    }
                    question = question.Remove(question.Length - 1);
                    writer.WriteLine(question);
                }
            }
        }
        static void DeduplicatedQuestion()
        {
            Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
            using (StreamReader reader = new StreamReader(@".\Model\QA.txt"))
            {
                while (!reader.EndOfStream)
                {
                    var parts = reader.ReadLine().Split('|').ToList();
                    if (dict.ContainsKey(parts[ 0 ]))
                    {
                        for (int i = 1; i < parts.Count; i++)
                        {
                            dict[parts[0]].Add(parts[i]);
                        }
                    }
                    else
                    {
                        dict.Add(parts[0], parts);
                    }
                }
            }
            using (StreamWriter writer = new StreamWriter(@".\Model\QA.txt"))
            {
                foreach (var item in dict)
                {
                    string line = "";
                    foreach (var subsentence in item.Value)
                    {
                        line += subsentence + "|";
                    }
                    line = line.Remove(line.Length - 1);
                    writer.WriteLine(line);
                }
            }
        }
        static double SumLargerThanThree1(List<double> numbers)
        {
            double retValue = 0;
            foreach (double number in numbers)
            {
                if (number > 3)
                {
                    retValue += number;
                }
            }
            return retValue;
        }
        static double SumLargerThanThree2(List<double> numbers)
        {
            List<int> oldList = Enumerable.Range(0, 10).ToList();
            List<int> newList = oldList.Select(x => x * x).ToList();
            foreach (int num in newList)
            {
                Console.WriteLine(num);
            }
            return numbers.Where(number => number > 3).Sum();
        }
        static void Main()
        {
            // GenerateConversationFile();
            // GeneratePrefixModel();
            // TrimStartSpace();
            // DeduplicatedQuestion();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormUI());
        }
    }
}
